package com.vincce.sport.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.SysResource;
import com.vincce.sport.mapper.SysResourceMapper;


@Controller
@RequestMapping("/sysResource")
public class SysResourceController extends BaseController{
	@Resource
	private SysResourceMapper sysResourceMapper;

    @RequestMapping("/list")
    public String sysResourceList(Model model, SysResource sysResource, Page<SysResource> page) throws IOException{
        Map<String, Object> filter = new HashMap<String, Object>();
        if(sysResource.getResourceName() != null && !sysResource.getResourceName().equals("")){
            filter.put("resourceName", sysResource.getResourceName());
        }
//		if(advertising.getStatus() != null && !advertising.getStatus().equals("")){
//			filter.put("status", advertising.getStatus());
//		}
        page.setFilter(filter);

        page.setResult(sysResourceMapper.querySysResourceList(page));
        page.setTotal(sysResourceMapper.querySysResourceCount(page.getFilter()));
        model.addAttribute("page",page);
        
        
        List<SysResource> sysResourceList = sysResourceMapper.findAllSysResource();
        model.addAttribute("sysResourceList",sysResourceList);
        
        return "backstage/sysResourceList";
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public String addOrUpdateSysResource(Model model, SysResource sysResource, HttpServletRequest request) throws IOException{
        Long count = 0l;
        //更新人和更新时间
        sysResource.setUpdateUser(getCurrentUser().getId().intValue());
        sysResource.setUpdateTime(new Date());
        if(CheckUtil.isEmpty(sysResource.getId())){
        	//创建 人和创建时间
        	sysResource.setCreateUser(getCurrentUser().getId().intValue());
        	sysResource.setCreateTime(new Date());

        	count = sysResourceMapper.insertSysResource(sysResource);
        } else{
        	count = sysResourceMapper.updateSysResource(sysResource);
        }

        this.sessionPutNodeResource(request);        //加载侧边条资源
        this.sessionPutResource(request);	//角色资源与全部资源放入session

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("suc", count > 0 ? true : false);
        return JSON.toJSONString(result);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String deleteSysResource(Model model, Long id, HttpServletRequest request) throws IOException{
        Boolean flag = false;

        sysResourceMapper.deleteById(id);

        this.sessionPutNodeResource(request);        //加载侧边条资源
        this.sessionPutResource(request);	//角色资源与全部资源放入session

        flag = true;
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("obj", flag);
        return JSON.toJSONString(result);
    }

    /**
     * 檢查資源名是否已存在
     * @param resourceName
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/checkResourceName")
    @ResponseBody
    public String checkResourceName(String resourceName,Long id) throws Exception{
        SysResource sysResource =new SysResource();
        if(resourceName != null && !resourceName.equals("")){
            sysResource.setResourceName(resourceName);
        }
        if(id!= null && !id.equals("")){
            sysResource.setId(id);
        }
        Integer fg = 0;
        List<SysResource> sysResourceList = sysResourceMapper.findSysResourceByOne(sysResource);
        if(sysResourceList!=null && sysResourceList.size()>0){
        	fg = 1;
        }
        return Boolean.toString(fg==0);
    }
}
